﻿namespace File_Renamer
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        delegate void DisplayUpdate();
        delegate void RenamerPrep();

        private Renamer renamer = new Renamer();
        private DisplayUpdate displayUpdater;
        //private RenamerPrep RenamerPreper;

        public MainWindow()
        {
            InitializeComponent();

            this.displayUpdater = new DisplayUpdate(this.DisplayResults);
            //this.RenamerPreper = new RenamerPrep(this.RenamerUiPrep);
        }

        private void RenamerUiPrep()
        {
            this.resultsBox1.Clear();
            this.resultsBox2.Clear();
            this.resultsBox3.Clear();

            this.resultsBox1.Text = "Working ...";

            // read in user inputs for renamer
            this.renamer.ShouldSuggestFix = (bool)this.autoFix.IsChecked;
            if ((bool)this.capsFix.IsChecked)
            {
                this.renamer.CapsFix = true;
            }
            this.renamer.RegEx = this.regex.Text;
            this.renamer.Folder = this.location.Text;
        }

        private void DisplayResults()
        {
            int globalResultsCount = 0;
            string resultsString1 = string.Empty;
            string resultsString2 = string.Empty;
            string resultsString3 = string.Empty;

            foreach (FileName file in this.renamer.Files)
            {
                resultsString1 += file.Location + Environment.NewLine;
                resultsString2 += file.Name + Environment.NewLine;

                if ((bool)this.autoFix.IsChecked)
                {
                    resultsString3 += file.NewName + Environment.NewLine;
                }

                globalResultsCount++;
            }

            this.resultsBox1.Text = resultsString1;
            this.resultsBox2.Text = resultsString2;
            this.resultsBox3.Text = resultsString3;
            this.resultsCount.Text = globalResultsCount.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PreviewButton_Click(object sender, RoutedEventArgs e)
        {
            this.SearchWithRenamer(false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeButton_Click(object sender, RoutedEventArgs e)
        {
            this.SearchWithRenamer(true);
        }

        /// <summary>
        /// Looks through the supplied folder location for files matching the regular expression, suggests fixes if necessary, and displays the results.
        /// </summary>
        private void SearchWithRenamer(bool shouldChangeFileNames)
        {
            this.RenamerUiPrep();
            Thread t = new Thread(this.SearchWithRenamerT);
            t.Start(shouldChangeFileNames);
        }

        /// <summary>
        /// 
        /// </summary>
        private void SearchWithRenamerT(object shouldChangeFileNames)
        {
            this.renamer.GetFileNames();

            if ((bool)shouldChangeFileNames)
            {
                this.renamer.ChangeFileNames();
            }

            this.resultsBox1.Dispatcher.BeginInvoke(this.displayUpdater);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.regex.Focus();
        }
    }
}
